const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isfalse: false, // 否
    starttime: '00:00', // 正在播放时长
    endtime: '00:00', //
    totalDuration: '00:00', // 总时长
    currentTime: 0, // 播放时长
    imgUrl: $util.getImgUrl(),
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(options);
  },
  onShow() {
    this.loadDetail();
  },
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/edu.train.getsecdetail',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        if (res.secdata && res.secdata.secaudio) {
          const arrs = res.secdata.secaudio.split('.');
          const arritem = arrs[arrs.length - 1].split('?');
          res.secdata.isvideo = arritem[0];
        }
        res.secdata.loaded = true;
        this.setData(res.secdata, () => {
          this.setData({ isLoad: true });
          if (res.secdata && res.secdata.secaudio && res.secdata.isvideo === 'mp3') {
            this.creatAudio();
          }
        });
        wx.setNavigationBarTitle({
          // title: res.secdata.sectitle || '',
          title: '章节详情',
        });
      },
    });
  },
  // 监听视频播放时间
  bindtimeupdate(e) {
    const {
      currentTime,
      duration,
    } = e.detail;
    this.setData({
      currentTime,
      duration,
    });
  },
  // 记录学习时长
  recordTime(e) {
    let isfinish = 0;
    if (this.data.duration - this.data.currentTime <= 1) {
      isfinish = 1;
    }
    $util.request({
      url: 'entry/wxapp/edu.train.setvideolength',
      data: {
        id: this.data.id,
        isfinish,
        slength: this.data.currentTime,
        tlength: this.data.duration,
      },
      success: () => {},
    });
  },
  audioPlay() {
    this.myaudio.play();
    this.setData({
      isplay: true,
    });
  },
  audioPaused() {
    this.myaudio.pause();
    this.setData({
      isplay: false,
    });
  },
  creatAudio() {
    this.myaudio = wx.createInnerAudioContext();
    this.myaudio.src = this.data.secaudio;
    this.myaudio.onWaiting((res) => {
      console.log('加载中');
      wx.showLoading({
        title: '正在缓冲...',
      });
    });
    this.myaudio.onCanplay((res) => {
      console.log('可以播放');
      wx.hideLoading();
    });
    this.myaudio.onPlay((res) => {
      console.log('监听到音频开始播放');
      wx.hideLoading();
    });
    this.myaudio.onTimeUpdate((res) => {
      // 当前时间
      let s = parseInt(this.myaudio.currentTime);
      let minute = parseInt(s / 60);
      if (minute < 10) {
        minute = '0' + minute;
      }
      let second = s % 60;
      if (second < 10) {
        second = '0' + second;
      }
      // 总时长
      let max = parseInt(this.myaudio.duration);
      let durMinute = parseInt(max / 60);
      if (durMinute < 10) {
        durMinute = '0' + durMinute;
      }
      let durSecond = max % 60;
      if (durSecond < 10) {
        durSecond = '0' + durSecond;
      }
      this.setData({
        starttime: minute + ':' + second,
        totalDuration: durMinute + ':' + durSecond, 
        currentTime: s,
        duration: max,
      });
    });
    this.myaudio.onEnded(() => {
      console.log('播放结束');
      this.setData({
        isplay: false,
      });
    });
    this.myaudio.onError((res) => {
      console.log('报错啦', res);
      let errMsg = '出错啦';
      this.setData({
        isplay: false,
      });
      this.myaudio.stop();
      switch (res.errCode) {
        case 10001:
          errMsg = '系统错误';
          break;
        case 10002:
          errMsg = '网络错误';
          break;
        case 10003:
          errMsg = '文件错误';
          break;
        case 10004:
          errMsg = '格式错误';
          break;
        default:
          errMsg = '未知错误';
      }
      wx.showToast({
        title: errMsg,
        icon: 'none',
      });
    });
  },
  sliderChange(e) {
    let currentTime = parseInt(e.detail.value);
    this.setData({
      currentTime, 
    });
    this.myaudio.seek(currentTime);
  },
  onHide() {
    this.recordTime();
  },
  onUnload() {
    if (this.data.isvideo === 'mp3') {
      this.myaudio.destroy();
    }
    this.recordTime();
  },
});